package com.example.hkConnection;

import com.example.commons.haikang.HaiKangEnum;
import com.example.commons.utils.constant.InternetInformation;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.Arrays;

/**
 * @Author 郅占伟
 * @ClassName HKConnection
 * @Description TODO
 * @Date 2024年05月15日
 * @Version: 1.0
 */
public class HKConnection {
    public static void main(String[] args) {
        String s = hKCameraPhoto();
    }
    
    public static String hKCameraPhoto(){
        try {
            Socket socket4001 = getSocket(InternetInformation.HOST_H_K_END_ADDRESS_A, InternetInformation.PORT_H_K_CONNECTION);
            OutputStream outputStream4001 = socket4001.getOutputStream();
            outputStream4001.write(HaiKangEnum.SET_ACQ_ONE.getCode().getBytes());
            outputStream4001.flush();

            // 开始扫码，写入到程序
            Socket socket2001 = getSocket(InternetInformation.HOST_H_K_END_ADDRESS_A, InternetInformation.PORT_H_K_PHOTO);
            OutputStream outputStream2001 = socket2001.getOutputStream();
            outputStream2001.write("start".getBytes());

            InputStream inputStream2001 = socket2001.getInputStream();
            byte[] photoBytes = new byte[1024];
            int read = inputStream2001.read(photoBytes);
            String photoMessage = new String(photoBytes, 0, read);

            socket4001.close();
            outputStream4001.close();
            socket2001.close();
            outputStream2001.close();
            inputStream2001.close();

            return photoMessage;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Socket getSocket(String host, int port) throws IOException {
        Socket socket = new Socket();
        SocketAddress socketAddress = new InetSocketAddress(host, port);
        socket.setSoTimeout(9000);
        socket.connect(socketAddress);
        return socket;
    }

    @Test
    public void testTcpConnection() {

    }
}
